package com.aurora.leetcode.answer;

import com.alibaba.fastjson.JSON;
import com.aurora.leetcode.util.MyUtils;

/**
 * @author : Aurora
 * @date : 2024/5/24 09:26
 * @description <p>344. 反转字符串</p>
 * <p>编写一个函数，其作用是将输入的字符串反转过来。输入字符串以字符数组 s 的形式给出。</p>
 * <p>不要给另外的数组分配额外的空间，你必须原地修改输入数组、使用 O(1) 的额外空间解决这一问题。</p>
 * <p>>>>>>example1<<<<<</p>
 * <p>输入：s = ["h","e","l","l","o"]</p>
 * <p>输出：["o","l","l","e","h"]</p>
 * <p>>>>>>example2<<<<<</p>
 * <p>输入：s = ["H","a","n","n","a","h"]</p>
 * <p>输出：["h","a","n","n","a","H"]</p>
 */
public class ReverseString_344 {
    public void reverseString(char[] s) {
        int left = 0;
        int right = s.length - 1;
        for (;left<right;++left,--right){
            char temp = s[left];
            s[left] = s[right];
            s[right] = temp;
        }
    }

    public static void main(String[] args) {
        ReverseString_344 obj = new ReverseString_344();
//        char[] s = new char[]{'h','e','l','l','o'};
        char[] s = new char[]{'H','a','n','n','a','h'};
        obj.reverseString(s);
        System.out.println(JSON.toJSONString(s));
    }
}
